﻿var companyId = '';
var ztId = '';
var kjnd = '';
var kjkm = '';
var xm = '';
var bm = '';
var aj = '';
var pjh = '';
var zph = '';
var wldx = '';

var userdefault = {};
var params = {};
var dataList = [];
var mapList = [{id: "col_pzid", field: "pzid"}, {id: "col_pzrq", field: "pzrq"}, {id: "col_pzh", field: "pzh"},
    {id: "col_flxh", field: "flxh"}, {id: "col_zy", field: "zy"}, {id: "col_j", field: "j"}, {id: "col_d", field: "d"},
    {id: "col_yefxStr", field: "yefxStr"}, {id: "col_ye", field: "ye"}];

//创建一个表格控件
var tbl = new QTable();

var bootstrap = function ($, learun) {

    var pageWidth = 1024;

    var _companyId = request("companyId");
    var _ztId = request("ztId");
    var _kjnd = request("kjnd");
    var _kjkm = request("kjkm");
    var _kjkmName = decodeURI(request("kjkmName"), "UTF-8");
    var _aj = decodeURI(request("aj"), "UTF-8");
    var _pjh = decodeURI(request("pjh"), "UTF-8");
    var _zph = decodeURI(request("zph"), "UTF-8");
    var _wldx = decodeURI(request("wldx"), "UTF-8");
    var _xm = decodeURI(request("xm"), "UTF-8");
    var _bm = decodeURI(request("bm"), "UTF-8");

    var _pzrqStart = request("pzrqStart");
    var _pzrqEnd = request("pzrqEnd");

    "use strict";
    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);
            if(!!_companyId){
                userdefault.companyId = _companyId;
            }
            if(!!_ztId){
                userdefault.ztId = _ztId;
            }
            if(!!_kjnd){
                userdefault.kjnd = _kjnd;
            }

            //初始化打印插件
            hiprint.init();
            pageWidth = ($('.lr-layout-grid').width());
            $("#divDataList").height($(window).height() - 180);

            tbl.BindDom("divDataList"); //绑定到模板
            tbl.setMaplist(mapList);  //设置数据映射关系
            page.bind();


        },
        bind: function () {
            $('#multiple_condition_query').lrMultipleQuery(function (queryJson) {
                page.search(queryJson);
            }, 400, 800);
            $('#pzrqMonth').lrselect({
                type: 'default',
                // 展开最大高度
                maxHeight: 300,
                placeholder: '月份',
                data: [
                    {'id': '01', 'text': '01'},
                    {'id': '02', 'text': '02'},
                    {'id': '03', 'text': '03'},
                    {'id': '04', 'text': '04'},
                    {'id': '05', 'text': '05'},
                    {'id': '06', 'text': '06'},
                    {'id': '07', 'text': '07'},
                    {'id': '08', 'text': '08'},
                    {'id': '09', 'text': '09'},
                    {'id': '10', 'text': '10'},
                    {'id': '11', 'text': '11'},
                    {'id': '12', 'text': '12'}
                ],
                select: function (item) {
                    if (!item || item.id == '-1') {
                        var beginDate = kjnd + "-01-01";
                        var day = new Date(kjnd, 12, 0);
                        var endDate = kjnd + "-" + 12 + "-" + day.getDate();
                        $("#pzrqStart").val(beginDate);
                        $("#pzrqEnd").val(endDate);
                    } else {
                        var kjqj = item.id;
                        var beginDate = kjnd + "-" + kjqj + "-01";
                        var day = new Date(kjnd, kjqj, 0);
                        var endDate = kjnd + "-" + kjqj + "-" + day.getDate();
                        $("#pzrqStart").val(beginDate);
                        $("#pzrqEnd").val(endDate);
                    }
                }
            });
            // 公司选择
            $('#company_select').lrselect({
                type: 'tree',
                // placeholder: '请选择机构',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                //allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;
                    }
                }
            });
            $('#company_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);
            // 帐套选择
            $('#zt_select').lrDataItemSelect({
                code: 'ZT', maxHeight: 100,
                select: function (item) {
                    if (!item || item.id == '-1') {
                        ztId = '';
                    } else {
                        ztId = item.id;
                        $("#zt_title").html(item.text);
                    }
                }
            }).lrselectSetFirst();

            // 凭证类型
            $('#pzlxCode').lrDataItemSelect({
                code: 'PZLXS', maxHeight: 100,placeholder: "==请选择凭证类型==",
                select: function (item) {

                }
            });

            // 会计年度选择
            $('#kjnd_select').lrDataItemSelect({
                code: 'YEAR', maxHeight: 100, placeholder: "==请选择年份==",
                select: function (item) {
                    if (!item || item.value == '-1') {
                        kjnd = '';
                    } else {
                        kjnd = item.value;

                        var beginDate = kjnd + "-01-01";
                        var day = new Date(kjnd, 12, 0);
                        var endDate = kjnd + "-12-" + day.getDate();
                        $("#pzrqStart").val(beginDate);
                        $("#pzrqEnd").val(endDate);


                        //凭证类型
                        $("#pzlxCode").lrselectRefresh({
                            url: config.base_server() + 'pzlxs/list?access_token=' + config.getToken().access_token,
                            param: {companyId: companyId, ztId: ztId, kjnd: kjnd, status: 1}
                        });
                        //会计科目
                        $('#kjkmCode').lrformselect({
                            placeholder: '请选择科目',
                            reset: true,
                            layerUrl: config.webportal_server + 'pages/finance/pz/kjkm/selectForm.html?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&controlName=kjkmCode&btnIndex=0' + '&needMx=0' + '&access_token=' + config.getToken().access_token,
                            layerUrlH: 750,
                            dataUrl: config.base_server() + 'kjkms/getName?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&access_token=' + config.getToken().access_token,
                            callback: function (kmItem) {
                                if (!kmItem || kmItem.value == '-1') {
                                    kjkm = {};
                                } else {
                                    kjkm = kmItem;
                                }
                            }
                        });
                        $('#xmCodes').lrformselect({
                            placeholder: '请选择项目',
                            reset: true,
                            layerUrl: config.webportal_server + 'pages/finance/xm/selectForm.html?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&controlName=xmCodes&btnIndex=0' + '&access_token=' + config.getToken().access_token,
                            layerUrlH: 750,
                            dataUrl: config.base_server() + 'xms/getName?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&access_token=' + config.getToken().access_token,
                            callback: function (xmItem) {
                                if (!xmItem || xmItem.value == '-1') {
                                    xm = {};
                                } else {
                                    xm = xmItem;
                                }
                            }
                        });
                        $('#bmCodes').lrformselect({
                            placeholder: '请选择部门',
                            reset: true,
                            layerUrl: config.webportal_server + 'pages/system/department/selectForm.html?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&controlName=bmCodes&btnIndex=0' + '&access_token=' + config.getToken().access_token,
                            dataUrl: config.base_server() + 'departments/getName?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&access_token=' + config.getToken().access_token,
                            layerUrlH: 750,
                            callback: function (deptItem) {
                                if (!deptItem || deptItem.value == '-1') {
                                    bm = {};
                                } else {
                                    bm = deptItem;
                                }
                            }
                        });
                        $('#ajCodes').lrformselect({
                            placeholder: '请选择案件',
                            enableInput: true,
                            reset: true,
                            layerUrl: config.webportal_server + 'pages/finance/aj/selectForm.html?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&controlName=ajCodes&btnIndex=0' + '&access_token=' + config.getToken().access_token,
                            layerUrlH: 750,
                            dataUrl: config.base_server() + 'ajs/getName?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&access_token=' + config.getToken().access_token,
                            callback: function (ajItem) {
                                if (!ajItem || ajItem.value == '-1') {
                                    aj = {};
                                } else {
                                    aj = ajItem;
                                }
                            }
                        });
                        $('#pjhCodes').lrformselect({
                            placeholder: '请选择票据号',
                            enableInput: true,
                            reset: true,
                            layerUrl: config.webportal_server + 'pages/finance/pjh/selectForm.html?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&type=1' + '&controlName=pjhCodes&btnIndex=0' + '&access_token=' + config.getToken().access_token,
                            layerUrlH: 750,
                            dataUrl: config.base_server() + 'pjhs/getName?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&type=1' + '&access_token=' + config.getToken().access_token,
                            callback: function (pjhItem) {
                                if (!pjhItem || pjhItem.value == '-1') {
                                    pjh = {};
                                } else {
                                    pjh = pjhItem;
                                }
                            }
                        });
                        $('#zphCodes').lrformselect({
                            placeholder: '请选择支票号',
                            enableInput: true,
                            reset: true,
                            layerUrl: config.webportal_server + 'pages/finance/pjh/selectForm.html?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&type=2' + '&controlName=zphCodes&btnIndex=0' + '&access_token=' + config.getToken().access_token,
                            layerUrlH: 750,
                            dataUrl: config.base_server() + 'pjhs/getName?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&type=2' + '&access_token=' + config.getToken().access_token,
                            callback: function (zphItem) {
                                if (!zphItem || zphItem.value == '-1') {
                                    zph = {};
                                } else {
                                    zph = zphItem;
                                }
                            }
                        });
                        $('#wldxCodes').lrformselect({
                            placeholder: '请选择往来对象',
                            enableInput: true,
                            reset: true,
                            layerUrl: config.webportal_server + 'pages/finance/wldx/selectForm.html?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&controlName=wldxCodes&btnIndex=0' + '&access_token=' + config.getToken().access_token,
                            layerUrlH: 750,
                            dataUrl: config.base_server() + 'wldxs/getName?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&access_token=' + config.getToken().access_token,
                            callback: function (wldxItem) {
                                if (!wldxItem || wldxItem.value == '-1') {
                                    wldx = {};
                                } else {
                                    wldx = wldxItem;
                                }
                            }
                        });
                        if (!!_companyId) {
                            companyId = _companyId;
                            ztId = _ztId;
                            kjnd = _kjnd;
                            var param = {};
                            if (!!_kjkm) {
                                var arr = _kjkm.split(' ');
                                kjkm = _kjkm;
                                param.kjkmCode = arr[0];
                            }
                            if (!!_aj) {
                                var arr = _aj.split(',');
                                param.ajCodes = _aj;
                                //aj = _aj;
                                $("#ajCodes_input").val(_aj)
                            }
                            if (!!_pjh) {
                                var arr = _pjh.split(',');
                                param.pjhCodes = _pjh;
                                //pjh = _pjh;
                                $("#pjhCodes_input").val(_pjh)
                            }
                            if (!!_zph) {
                                var arr = _zph.split(',');
                                param.zphCodes = _zph;
                                //zph = _zph;
                                $("#zphCodes_input").val(_zph)
                            }
                            if (!!_wldx) {
                                var arr = _wldx.split(',');
                                param.wldxCodes = _wldx;
                                //wldx = _wldx;
                                $("#wldxCodes_input").val(_wldx)
                            }
                            if (!!_xm) {
                                var arr = _xm.split(',');
                                param.xmCodes = _xm;
                                //xm = _xm;
                            }
                            if (!!_bm) {
                                var arr = _bm.split(',');
                                param.bmCodes = _bm;
                                //bm = _bm;
                            }
                            if (!!_pzrqStart) {
                                $("#pzrqStart").val(_pzrqStart);
                            }
                            if (!!_pzrqEnd) {
                                $("#pzrqEnd").val(_pzrqEnd);
                            }
                            page.search(param);
                        } else {
                            page.search({});
                        }
                    }
                }
            });

            // 查询
            $('#btn_Search').on('click', function () {
                page.search(params);
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });

            //预览
            $('#lr_printview').on('click', function () {
                var hiprintTemplate = null;

                learun.httpAsyncGet2(config.base_server() + 'printTempletes/findByCode', "&companyId=" + companyId + '&ztId=' + ztId + '&code=slmxz', function (res) {
                    if (res.resp_code == 200) {
                        hiprintTemplate = new hiprint.PrintTemplate({
                            template: JSON.parse(res.data.templete),
                            paginationContainer: '.hiprint-printPagination'
                        });

                        //hiprintTemplate.design('#hiprint-printTemplate');

                        var item = {};
                        item.pzflList = dataList;
                        if (item.pzflList.length > 38) {
                            item.pzflList = item.pzflList.slice(0, 72);
                        }

                        item.ztName = $("#zt_select .lr-select-placeholder").html();
                        item.kjkmName = kjkm.text;
                        var pzrqStart = $("#pzrqStart").val();
                        var pzrqEnd = $("#pzrqEnd").val();
                        item.pzrq = pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10);
                        //hiprintTemplate.print(item);
                        hiprintTemplate.resetPage();
                        $('#myModal .modal-body .prevViewDiv').html(hiprintTemplate.getHtml(item));
                        $('#myModal').modal('show');
                    }
                });
                $('#A4_printByHtml').click(function () {
                    hiprintTemplate.printByHtml($('#myModal .modal-body .prevViewDiv'));
                })
            });

            //打印
            $('#lr_print').on('click', function () {
                var hiprintTemplate = null;

                learun.httpAsyncGet2(config.base_server() + 'printTempletes/findByCode', "&companyId=" + companyId + '&ztId=' + ztId + '&code=slmxz', function (res) {
                    if (res.resp_code == 200) {
                        hiprintTemplate = new hiprint.PrintTemplate({
                            template: JSON.parse(res.data.templete),
                            paginationContainer: '.hiprint-printPagination'
                        });

                        //hiprintTemplate.design('#hiprint-printTemplate');

                        var item = {};
                        item.pzflList = dataList;

                        item.ztName = $("#zt_select .lr-select-placeholder").html();
                        item.kjkmName = kjkm.text;
                        var pzrqStart = $("#pzrqStart").val();
                        var pzrqEnd = $("#pzrqEnd").val();
                        item.pzrq = pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10);
                        hiprintTemplate.resetPage();
                        hiprintTemplate.print2(item);
                    }
                });
            });

            //导出Excel
            $("#lr_export_excel").on('click', function () {
                if (params.companyId == undefined) {
                    learun.alert.warning('请先查询要导出的数据！');
                    return;
                }
                params.access_token = config.getToken().access_token;

                learun.download({
                    url: config.base_server() + 'reports/slmxz/excel',
                    param: params
                });
            });
            //导出pdf
            $("#lr_export_pdf").on('click', function () {
                if (params.companyId == undefined) {
                    learun.alert.warning('请先查询要导出的数据！');
                    return;
                }
                params.access_token = config.getToken().access_token;

                learun.download({
                    url: config.base_server() + 'reports/slmxz/pdf',
                    param: params
                });
            });
        },


        search: function (param) {
            param = param || {};
            var pzrqStart = $("#pzrqStart").val();
            var pzrqEnd = $("#pzrqEnd").val();
            param.companyId = companyId;
            param.ztId = ztId;
            param.kjnd = kjnd;
            param.pzrqStart = pzrqStart;
            param.pzrqEnd = pzrqEnd;
            var title = "";
            if (!kjkm) {
                learun.alert.warning('请选择科目！');
                return;
            }
            if (!!_kjkmName) {//联查
                title += _kjkmName;
            } else {
                title += kjkm.text;
            }

            if (!!$("#ajCodes_input").val()) {
                title += "  案件：" + $("#ajCodes_input").val();
            }
            if (!!$("#pjhCodes_input").val()) {
                title += "  票据号：" + $("#pjhCodes_input").val();
            }
            if (!!$("#zphCodes_input").val()) {
                title += "  支票号：" + $("#zphCodes_input").val();
            }
            if (!!$("#wldxCodes_input").val()) {
                title += "  往来对象：" + $("#wldxCodes_input").val();
            }
            if (!!xm) {
                title += "  项目：" + xm.text;
            }
            if (!!bm) {
                title += "  部门：" + bm.text;
            }
            if ($("#ajCodes_input").val().trim() == "") {
                param.ajCodes = "";
            }
            if ($("#pjhCodes_input").val().trim() == "") {
                param.pjhCodes = "";
            }
            if ($("#wldxCodes_input").val().trim() == "") {
                param.wldxCodes = "";
            }
            params = param;
            learun.loading(true);
            learun.httpPost2(config.base_server() + 'reports/slmxz?access_token=' + config.getToken().access_token, param, function (res) {
                learun.loading(false);
                if (res.resp_code == 200) {
                    dataList = res.data;
                    for (var i = 0; i < dataList.length; i++) {
                        dataList[i].j = $.formatMoney(dataList[i].j, 2);
                        dataList[i].d = $.formatMoney(dataList[i].d, 2);
                        dataList[i].ye = $.formatMoney(dataList[i].ye, 2);
                    }
                    tbl.BindData(dataList);
                } else {
                    learun.alert.error(res.resp_msg);
                    learun.httpErrorLog(res.resp_msg);
                }
            });

            $("#pzrq_title").html(pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10));
            $("#kjkm_title").html(title);
        }
    };
    page.init();
}

//查看凭证
function ViewData(row) {
    var data = row.data;
    var keyValue = data.pzid;
    var flxh = data.flxh;
    if (keyValue) {
        top.learun.frameTab.open({
            id: 'pz_view' + data.pzh,
            css: 'fa fa-file-text-o',
            name: '查看凭证' + data.pzh,
            path: config.webportal_server + 'pages/finance/pz/view.html?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&keyValue=' + keyValue + '&flxh=' + flxh + '&access_token=' + config.getToken().access_token
        });
    }
}
